package com.xlh.service.course.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xlh.dto.course.ChapterBasicInfoDTO;
import com.xlh.dto.course.CourseBasicDTO;
import com.xlh.enums.course.ImageLoadTypeEnum;
import com.xlh.service.course.ChapterExtendService;
import com.xlh.service.course.ChapterService;
import com.xlh.service.course.CourseService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 课程
 * <p>
 * Created by lx on 2019/1/28.
 */
@Service
public class ChapterExtendServiceImpl implements ChapterExtendService {

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private CourseService courseService;

    @Override
    public Map<String, Object> getCourseInfo(Long taskId) {
        // 获取任务信息
        List<ChapterBasicInfoDTO> tasks = chapterService.listChapterInfo(
                null, Lists.newArrayList(taskId), null);
        if (CollectionUtils.isEmpty(tasks)) return null;

        // 获取章节信息
        List<ChapterBasicInfoDTO> chapters = chapterService.listChapterInfo(
                null, Lists.newArrayList(tasks.get(0).getParentId()), null);
        if (CollectionUtils.isEmpty(chapters)) return null;

        try {
            // 获取课程信息
            CourseBasicDTO course = courseService.getCourse(chapters.get(0).getCourseId());
            ImageLoadTypeEnum imageLoadTypeEnum = ImageLoadTypeEnum.getByCode(course.getImageLoadType());

            Map<String, Object> result = Maps.newHashMap();
            result.put("courseImageLoaded", imageLoadTypeEnum == ImageLoadTypeEnum.COURSE ? true : false);
            if (imageLoadTypeEnum == ImageLoadTypeEnum.COURSE) {
                result.put("courseId", course.getId());
            }
            return result;

        } catch (Exception e) {
            return null;
        }

    }

}
